exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
    constructor(user) {
        super();
        const db = this.db;
        this.eventModel = db['cms_event'];
        this.attachmentModel = db['sys_attachment'];
        this.user = user;
    }

    /**
     * 违法抓拍上报事件
     * userid --上报用户
     * baidu_x --经度
     * baidu_y --纬度
     * source --事件来源
     * realpath --图片路径
     */
    saveEventByVideo(data) {
        var _this = this;

        return _asyncToGenerator(function* () {
            const eventData = {
                create_by: data.userid,
                update_by: data.userid,
                create_date: new Date(),
                update_date: new Date(),
                source: data.source,
                remarks: data.remarks,
                baidu_x: data.baidu_x,
                baidu_y: data.baidu_y,
                ObjPosition: data.ObjPosition,
                id: think.uuid('v1')
            };
            const attachmentData = {
                id: think.uuid('v1'),
                businesskey: eventData.id,
                realpath: data.realpath,
                create_date: eventData.create_date,
                user_id: data.userid
            };
            try {
                const event = yield _this.eventModel.create(eventData);
                const attachment = yield _this.attachmentModel.create(attachmentData);
                return JSON.parse(JSON.stringify(event));
            } catch (err) {
                return _this.exceptionService.handleError(err);
            }
        })();
    }

    /**
     * 获取摄像头及目录列表
     * @returns {Promise<*>}
     */
    getCatalogList() {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            const Res = yield _this2.CrudService.findAll('device_catalog_video', { is_enable: 1 });
            const defauType = Res.filter(function (item) {
                return item.type !== 2;
            });
            const type2 = Res.filter(function (item) {
                return item.type === 2;
            });
            for (let index = 0; index < type2.length; index++) {
                const sql = `SELECT * FROM device_catalog_video WHERE parent_ids LIKE '${type2[index].parent_ids + type2[index].device_id}%'`;
                const count = yield _this2.CrudService.querySql(sql);
                type2[index].count = count.length;
            }
            return [...defauType, ...type2];
        })();
    }
};